package com.cennavi.bigdata.baotou.scripts;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.util.DateUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.*;

import static java.lang.System.out;

/**
 * Created by zhouxiaolong on 2017/3/21.
 */
public class ActualTimeRoadIndex extends ActualTimeRequest {

    private DBCollection roadIndexCol;
    private static final Logger LOGGER = LoggerFactory.getLogger(ActualTimeRoadIndex.class);

    /**
     * 刷新行政区域指数数据
     */
    public void refrashRoadIndexRT(){
        roadIndexCol = getMongoCollection("road_index_history");
        String RoadIndexRTUrl = propObj.getString("DCIndexRT");
        String strings = sendGet(RoadIndexRTUrl);
        LOGGER.info("刷新行政区域指数数据");
        JSONArray objects = JSONArray.parseArray(strings);
        for (Object obj:objects) {
            JSONObject jsonObject = JSONObject.parseObject(obj.toString());
            //{"dccode":"150298","dcname":"市中心","latlon":"110.09785,40.58643","c_index":4.3,"avgspeed":28.2}
            String dccode = jsonObject.getString("dccode");
            if (dccode.equals("150298")) {
                String newTime = DateUtils.DateFormatUnit.DATE_AND_MIN.getDateStr(DateUtils.addMinute(new Date(),-1));
                DBCursor cursor = roadIndexCol.find().sort(new BasicDBObject("update_time", -1)).limit(1);
                while (cursor.hasNext()) {
                    DBObject constructionOccupation = cursor.next();
                    String lastTime = (String) constructionOccupation.get("update_time");
                    try {
                        makeTestData(DateUtils.addMinute(DateUtils.DateFormatUnit.DATE_AND_MIN.getDateByStr(lastTime),5),DateUtils.DateFormatUnit.DATE_AND_MIN.getDateByStr(newTime));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if(jsonObject.getDouble("c_index").compareTo(new Double(10))>0){
                    jsonObject.put("c_index",10.0);
                }
                jsonObject.put("update_time",newTime);
                jsonObject.put("c_index", Double.valueOf(new DecimalFormat("#.00").format(Double.parseDouble(jsonObject.getString("c_index")))));
                jsonObject.put("avgspeed",Double.valueOf(new DecimalFormat("#.00").format(Double.parseDouble(jsonObject.getString("avgspeed")))));
                roadIndexCol.insert(BasicDBObject.parse(jsonObject.toJSONString()));
                closeMongo();
                break;
            }
        }
    }

    private void makeTestData(Date todayStartTime,Date todayEndTime) throws Exception {
        //插入测试数据
        out.println("insert test data......");
        while(DateUtils.earlierThan(todayStartTime,todayEndTime)){
            String strings = "{\"dccode\":\"150298\",\"dcname\":\"市中心\",\"latlon\":\"110.09785,40.58643\",\"c_index\":2.1,\"avgspeed\":41.5}";
            JSONObject jsonObject = JSONObject.parseObject(strings);
            //{"dccode":"150298","dcname":"市中心","latlon":"110.09785,40.58643","c_index":4.3,"avgspeed":28.2}
            jsonObject.put("update_time",DateUtils.DateFormatUnit.DATE_AND_MIN.getDateStr(todayStartTime));
            roadIndexCol.insert(BasicDBObject.parse(jsonObject.toJSONString()));
            todayStartTime = DateUtils.addMinute(todayStartTime,5);
        }
    }

    /**
     * 删除俩周前的行政区域指数数据
     */
    public void removeRoadIndexRT(){
        roadIndexCol = getMongoCollection("road_index_history");
        LOGGER.info("删除俩周前行政区域指数数据");
        BasicDBObject cond = new BasicDBObject();
        cond.put("$lt", DateUtils.DateFormatUnit.DATE_AND_MIN.getDateStr(DateUtils.addDays(new Date(), -14)));
        BasicDBObject query = new BasicDBObject();
        query.put("update_time",cond);
        roadIndexCol.remove(query);
        closeMongo();
    }
}
